const router = require('koa-router')()

router.get('/', async (ctx, next) => {
  await ctx.render('index', {
    title: 'Hello Koa 2!'
  })
})

router.get('/getdata', async (ctx, next) => {
  // console.log('received request');
  ctx.body = 'received request'
})

router.get('/getlist', async (ctx, next) => {
  let list = [
    {
      chapterId: '1',
      chapterTitle: 'bala',
      timer: '2023-3-03'
    },
    {
      chapterId: '2',
      chapterTitle: 'balabala',
      timer: '2023-3-03'
    },
    {
      chapterId: '3',
      chapterTitle: 'balabalabala',
      timer: '2023-3-03'
    },
  ]
  ctx.body = list
})

router.get('ring', async (ctx, next) => {
  ctx.body = 'koa2 string'
})

router.get('/json', async (ctx, next) => {
  ctx.body = {
    title: 'koa2 json'
  }
})

router.post('/sub', async (ctx, next) => {
  console.log(ctx.request.body);
  let chapterTitle = ctx.request.body.chapter
  let chapterObj = {
    chapterId: list.length,
    chapterTitle: chapterTitle,
    chapterTimer: '2023-03-03'
  }
  list.push(chapterObj)
  ctx.body = 'ok'
})

module.exports = router